const api = require("../../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    estimationData: {
      feedbackCode: "",
      evaluateScore: '',
      evaluateContent: '',
      evaluateSource: 1,
    },
  },
  onBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  evaluateScore: function (e) {
    const score = e.currentTarget.dataset.score; // 获取点击的评分值
    this.setData({
      'estimationData.evaluateScore': score // 更新选中的评分
    });
    console.log('选择', this.data.estimationData.evaluateScore)
  },
  onClickSubmit: function (e) {
    console.log(this.data)
    wx.request({
      url: api.evaluateAdd,
      method: 'POST',
      data: Object.assign({}, this.data.estimationData), // 平铺
      success: (res) => {
        console.log("评价结果信息：")
        console.log(res.data)
        if (res.data.code == 200) {
          console.log("评价结果信息：")
          console.log(res.data.data)
          wx.showToast({
            title: '提交成功',
          })
          wx.navigateBack({
            delta: 1
          });
        } else {
          wx.showToast({
            title: res.data.mesage,
            icon: "none"
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const feedbackCode = options.feedbackCode;
    const workOrderCode = options.workOrderCode;
    console.log('传递feedbackCode:', feedbackCode);
    console.log('传递workOrderCode:', workOrderCode);
    this.setData({
      'estimationData.feedbackCode': feedbackCode,
      'estimationData.workOrderCode': workOrderCode,
    });
    // 获取工单信息
    wx.request({
      url: api.workOrderInfo,
      method: 'POST',
      data: {
        workOrderCode,
      },
      success: (res) => {
        console.log("工单信息：")
        console.log(res.data)
        if (res.data.code == 200) {
          console.log("工单信息：")
          console.log(res.data.data)
          const resData = res.data.data;
          this.setData({
            'estimationData.electricianName':resData.electricianName,
            'estimationData.electricianCode':resData.electricianCode
          });
        } else {
          wx.showToast({
            title: res.data.mesage,
            icon: "none"
          })
        }
      }
    })
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        wx.request({
          url: api.customerInfo,
          method: 'POST',
          data: {
            customerCode: res.data,
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              const resData = res.data.data;
              this.setData({
                'estimationData.evaluateCustomerName': resData.customerName,
                'estimationData.evaluateSourceCode': resData.customerCode,
                'estimationData.evaluateSourceName': resData.customerUserName,
              });
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },
  // 输入评价内容
  inputContent(e) {
    this.setData({
      'estimationData.evaluateContent': e.detail.value
    })
  },
})